package com.qf.oop.extend;

/**
 * 演示类型转换
 */
public class Demo01 {

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.sleep();
        dog.eat();
        dog.run();
        Animal animal = dog; // 向上转型
        animal.eat();
        animal.sleep();
        Dog d = (Dog) animal; // 向下转型，需要添加强制转换的语法
        d.sleep();
        d.eat();
        d.run();
        // instanseof 关键字的作用
        if (animal instanceof Fish){
            Fish fish = (Fish) animal;  // ?
            fish.sleep();
            System.out.println(fish.pianColor);
        }else if (animal instanceof Cat){
            Cat cat = (Cat) animal;
            cat.eat();
        }else {
            System.out.println("aninmal 的真实类型是Dog");
            Dog dog2 = (Dog) animal;
            dog2.eat();
        }


    }

}
